package qldc.pm.tienan.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.tienan.NoSuchTEMP_TienAnException;
import qldc.pm.tienan.model.TEMP_TienAn;
import qldc.pm.tienan.model.impl.TEMP_TienAnImpl;
import qldc.pm.tienan.model.impl.TEMP_TienAnModelImpl;
import qldc.pm.tienan.service.persistence.TEMP_TienAnPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the t e m p_ tien an service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see TEMP_TienAnPersistence
 * @see TEMP_TienAnUtil
 * @generated
 */
public class TEMP_TienAnPersistenceImpl extends BasePersistenceImpl<TEMP_TienAn>
    implements TEMP_TienAnPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link TEMP_TienAnUtil} to access the t e m p_ tien an persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = TEMP_TienAnImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnModelImpl.FINDER_CACHE_ENABLED, TEMP_TienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnModelImpl.FINDER_CACHE_ENABLED, TEMP_TienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_DOITUONGTIENANID =
        new FinderPath(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnModelImpl.FINDER_CACHE_ENABLED, TEMP_TienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByDoiTuongTienAnId",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENANID =
        new FinderPath(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnModelImpl.FINDER_CACHE_ENABLED, TEMP_TienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByDoiTuongTienAnId", new String[] { Long.class.getName() },
            TEMP_TienAnModelImpl.DOITUONGTIENANID_COLUMN_BITMASK |
            TEMP_TienAnModelImpl.CREATEDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_DOITUONGTIENANID = new FinderPath(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByDoiTuongTienAnId", new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_DOITUONGTIENANID_DOITUONGTIENANID_2 =
        "temp_TienAn.doiTuongTienAnId = ?";
    private static final String _SQL_SELECT_TEMP_TIENAN = "SELECT temp_TienAn FROM TEMP_TienAn temp_TienAn";
    private static final String _SQL_SELECT_TEMP_TIENAN_WHERE = "SELECT temp_TienAn FROM TEMP_TienAn temp_TienAn WHERE ";
    private static final String _SQL_COUNT_TEMP_TIENAN = "SELECT COUNT(temp_TienAn) FROM TEMP_TienAn temp_TienAn";
    private static final String _SQL_COUNT_TEMP_TIENAN_WHERE = "SELECT COUNT(temp_TienAn) FROM TEMP_TienAn temp_TienAn WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "temp_TienAn.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No TEMP_TienAn exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No TEMP_TienAn exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(TEMP_TienAnPersistenceImpl.class);
    private static TEMP_TienAn _nullTEMP_TienAn = new TEMP_TienAnImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<TEMP_TienAn> toCacheModel() {
                return _nullTEMP_TienAnCacheModel;
            }
        };

    private static CacheModel<TEMP_TienAn> _nullTEMP_TienAnCacheModel = new CacheModel<TEMP_TienAn>() {
            @Override
            public TEMP_TienAn toEntityModel() {
                return _nullTEMP_TienAn;
            }
        };

    public TEMP_TienAnPersistenceImpl() {
        setModelClass(TEMP_TienAn.class);
    }

    /**
     * Returns all the t e m p_ tien ans where doiTuongTienAnId = &#63;.
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @return the matching t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienAn> findByDoiTuongTienAnId(long doiTuongTienAnId)
        throws SystemException {
        return findByDoiTuongTienAnId(doiTuongTienAnId, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the t e m p_ tien ans where doiTuongTienAnId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_TienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @param start the lower bound of the range of t e m p_ tien ans
     * @param end the upper bound of the range of t e m p_ tien ans (not inclusive)
     * @return the range of matching t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienAn> findByDoiTuongTienAnId(long doiTuongTienAnId,
        int start, int end) throws SystemException {
        return findByDoiTuongTienAnId(doiTuongTienAnId, start, end, null);
    }

    /**
     * Returns an ordered range of all the t e m p_ tien ans where doiTuongTienAnId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_TienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @param start the lower bound of the range of t e m p_ tien ans
     * @param end the upper bound of the range of t e m p_ tien ans (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienAn> findByDoiTuongTienAnId(long doiTuongTienAnId,
        int start, int end, OrderByComparator orderByComparator)
        throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENANID;
            finderArgs = new Object[] { doiTuongTienAnId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_DOITUONGTIENANID;
            finderArgs = new Object[] {
                    doiTuongTienAnId,
                    
                    start, end, orderByComparator
                };
        }

        List<TEMP_TienAn> list = (List<TEMP_TienAn>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (TEMP_TienAn temp_TienAn : list) {
                if ((doiTuongTienAnId != temp_TienAn.getDoiTuongTienAnId())) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 +
                        (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_TEMP_TIENAN_WHERE);

            query.append(_FINDER_COLUMN_DOITUONGTIENANID_DOITUONGTIENANID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);
            } else
             if (pagination) {
                query.append(TEMP_TienAnModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(doiTuongTienAnId);

                if (!pagination) {
                    list = (List<TEMP_TienAn>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TEMP_TienAn>(list);
                } else {
                    list = (List<TEMP_TienAn>) QueryUtil.list(q, getDialect(),
                            start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first t e m p_ tien an in the ordered set where doiTuongTienAnId = &#63;.
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching t e m p_ tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_TienAnException if a matching t e m p_ tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn findByDoiTuongTienAnId_First(long doiTuongTienAnId,
        OrderByComparator orderByComparator)
        throws NoSuchTEMP_TienAnException, SystemException {
        TEMP_TienAn temp_TienAn = fetchByDoiTuongTienAnId_First(doiTuongTienAnId,
                orderByComparator);

        if (temp_TienAn != null) {
            return temp_TienAn;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("doiTuongTienAnId=");
        msg.append(doiTuongTienAnId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTEMP_TienAnException(msg.toString());
    }

    /**
     * Returns the first t e m p_ tien an in the ordered set where doiTuongTienAnId = &#63;.
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching t e m p_ tien an, or <code>null</code> if a matching t e m p_ tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn fetchByDoiTuongTienAnId_First(long doiTuongTienAnId,
        OrderByComparator orderByComparator) throws SystemException {
        List<TEMP_TienAn> list = findByDoiTuongTienAnId(doiTuongTienAnId, 0, 1,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last t e m p_ tien an in the ordered set where doiTuongTienAnId = &#63;.
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching t e m p_ tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_TienAnException if a matching t e m p_ tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn findByDoiTuongTienAnId_Last(long doiTuongTienAnId,
        OrderByComparator orderByComparator)
        throws NoSuchTEMP_TienAnException, SystemException {
        TEMP_TienAn temp_TienAn = fetchByDoiTuongTienAnId_Last(doiTuongTienAnId,
                orderByComparator);

        if (temp_TienAn != null) {
            return temp_TienAn;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("doiTuongTienAnId=");
        msg.append(doiTuongTienAnId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTEMP_TienAnException(msg.toString());
    }

    /**
     * Returns the last t e m p_ tien an in the ordered set where doiTuongTienAnId = &#63;.
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching t e m p_ tien an, or <code>null</code> if a matching t e m p_ tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn fetchByDoiTuongTienAnId_Last(long doiTuongTienAnId,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByDoiTuongTienAnId(doiTuongTienAnId);

        if (count == 0) {
            return null;
        }

        List<TEMP_TienAn> list = findByDoiTuongTienAnId(doiTuongTienAnId,
                count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the t e m p_ tien ans before and after the current t e m p_ tien an in the ordered set where doiTuongTienAnId = &#63;.
     *
     * @param tienAnId the primary key of the current t e m p_ tien an
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next t e m p_ tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_TienAnException if a t e m p_ tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn[] findByDoiTuongTienAnId_PrevAndNext(long tienAnId,
        long doiTuongTienAnId, OrderByComparator orderByComparator)
        throws NoSuchTEMP_TienAnException, SystemException {
        TEMP_TienAn temp_TienAn = findByPrimaryKey(tienAnId);

        Session session = null;

        try {
            session = openSession();

            TEMP_TienAn[] array = new TEMP_TienAnImpl[3];

            array[0] = getByDoiTuongTienAnId_PrevAndNext(session, temp_TienAn,
                    doiTuongTienAnId, orderByComparator, true);

            array[1] = temp_TienAn;

            array[2] = getByDoiTuongTienAnId_PrevAndNext(session, temp_TienAn,
                    doiTuongTienAnId, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected TEMP_TienAn getByDoiTuongTienAnId_PrevAndNext(Session session,
        TEMP_TienAn temp_TienAn, long doiTuongTienAnId,
        OrderByComparator orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(6 +
                    (orderByComparator.getOrderByFields().length * 6));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_TEMP_TIENAN_WHERE);

        query.append(_FINDER_COLUMN_DOITUONGTIENANID_DOITUONGTIENANID_2);

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(TEMP_TienAnModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(doiTuongTienAnId);

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(temp_TienAn);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<TEMP_TienAn> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the t e m p_ tien ans where doiTuongTienAnId = &#63; from the database.
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByDoiTuongTienAnId(long doiTuongTienAnId)
        throws SystemException {
        for (TEMP_TienAn temp_TienAn : findByDoiTuongTienAnId(
                doiTuongTienAnId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(temp_TienAn);
        }
    }

    /**
     * Returns the number of t e m p_ tien ans where doiTuongTienAnId = &#63;.
     *
     * @param doiTuongTienAnId the doi tuong tien an ID
     * @return the number of matching t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByDoiTuongTienAnId(long doiTuongTienAnId)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_DOITUONGTIENANID;

        Object[] finderArgs = new Object[] { doiTuongTienAnId };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_TEMP_TIENAN_WHERE);

            query.append(_FINDER_COLUMN_DOITUONGTIENANID_DOITUONGTIENANID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(doiTuongTienAnId);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the t e m p_ tien an in the entity cache if it is enabled.
     *
     * @param temp_TienAn the t e m p_ tien an
     */
    @Override
    public void cacheResult(TEMP_TienAn temp_TienAn) {
        EntityCacheUtil.putResult(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnImpl.class, temp_TienAn.getPrimaryKey(), temp_TienAn);

        temp_TienAn.resetOriginalValues();
    }

    /**
     * Caches the t e m p_ tien ans in the entity cache if it is enabled.
     *
     * @param temp_TienAns the t e m p_ tien ans
     */
    @Override
    public void cacheResult(List<TEMP_TienAn> temp_TienAns) {
        for (TEMP_TienAn temp_TienAn : temp_TienAns) {
            if (EntityCacheUtil.getResult(
                        TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
                        TEMP_TienAnImpl.class, temp_TienAn.getPrimaryKey()) == null) {
                cacheResult(temp_TienAn);
            } else {
                temp_TienAn.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all t e m p_ tien ans.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(TEMP_TienAnImpl.class.getName());
        }

        EntityCacheUtil.clearCache(TEMP_TienAnImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the t e m p_ tien an.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(TEMP_TienAn temp_TienAn) {
        EntityCacheUtil.removeResult(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnImpl.class, temp_TienAn.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @Override
    public void clearCache(List<TEMP_TienAn> temp_TienAns) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (TEMP_TienAn temp_TienAn : temp_TienAns) {
            EntityCacheUtil.removeResult(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
                TEMP_TienAnImpl.class, temp_TienAn.getPrimaryKey());
        }
    }

    /**
     * Creates a new t e m p_ tien an with the primary key. Does not add the t e m p_ tien an to the database.
     *
     * @param tienAnId the primary key for the new t e m p_ tien an
     * @return the new t e m p_ tien an
     */
    @Override
    public TEMP_TienAn create(long tienAnId) {
        TEMP_TienAn temp_TienAn = new TEMP_TienAnImpl();

        temp_TienAn.setNew(true);
        temp_TienAn.setPrimaryKey(tienAnId);

        return temp_TienAn;
    }

    /**
     * Removes the t e m p_ tien an with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param tienAnId the primary key of the t e m p_ tien an
     * @return the t e m p_ tien an that was removed
     * @throws qldc.pm.tienan.NoSuchTEMP_TienAnException if a t e m p_ tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn remove(long tienAnId)
        throws NoSuchTEMP_TienAnException, SystemException {
        return remove((Serializable) tienAnId);
    }

    /**
     * Removes the t e m p_ tien an with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the t e m p_ tien an
     * @return the t e m p_ tien an that was removed
     * @throws qldc.pm.tienan.NoSuchTEMP_TienAnException if a t e m p_ tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn remove(Serializable primaryKey)
        throws NoSuchTEMP_TienAnException, SystemException {
        Session session = null;

        try {
            session = openSession();

            TEMP_TienAn temp_TienAn = (TEMP_TienAn) session.get(TEMP_TienAnImpl.class,
                    primaryKey);

            if (temp_TienAn == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchTEMP_TienAnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(temp_TienAn);
        } catch (NoSuchTEMP_TienAnException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected TEMP_TienAn removeImpl(TEMP_TienAn temp_TienAn)
        throws SystemException {
        temp_TienAn = toUnwrappedModel(temp_TienAn);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(temp_TienAn)) {
                temp_TienAn = (TEMP_TienAn) session.get(TEMP_TienAnImpl.class,
                        temp_TienAn.getPrimaryKeyObj());
            }

            if (temp_TienAn != null) {
                session.delete(temp_TienAn);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (temp_TienAn != null) {
            clearCache(temp_TienAn);
        }

        return temp_TienAn;
    }

    @Override
    public TEMP_TienAn updateImpl(qldc.pm.tienan.model.TEMP_TienAn temp_TienAn)
        throws SystemException {
        temp_TienAn = toUnwrappedModel(temp_TienAn);

        boolean isNew = temp_TienAn.isNew();

        TEMP_TienAnModelImpl temp_TienAnModelImpl = (TEMP_TienAnModelImpl) temp_TienAn;

        Session session = null;

        try {
            session = openSession();

            if (temp_TienAn.isNew()) {
                session.save(temp_TienAn);

                temp_TienAn.setNew(false);
            } else {
                session.merge(temp_TienAn);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !TEMP_TienAnModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }
        else {
            if ((temp_TienAnModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENANID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        temp_TienAnModelImpl.getOriginalDoiTuongTienAnId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_DOITUONGTIENANID,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENANID,
                    args);

                args = new Object[] { temp_TienAnModelImpl.getDoiTuongTienAnId() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_DOITUONGTIENANID,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENANID,
                    args);
            }
        }

        EntityCacheUtil.putResult(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienAnImpl.class, temp_TienAn.getPrimaryKey(), temp_TienAn);

        return temp_TienAn;
    }

    protected TEMP_TienAn toUnwrappedModel(TEMP_TienAn temp_TienAn) {
        if (temp_TienAn instanceof TEMP_TienAnImpl) {
            return temp_TienAn;
        }

        TEMP_TienAnImpl temp_TienAnImpl = new TEMP_TienAnImpl();

        temp_TienAnImpl.setNew(temp_TienAn.isNew());
        temp_TienAnImpl.setPrimaryKey(temp_TienAn.getPrimaryKey());

        temp_TienAnImpl.setTienAnId(temp_TienAn.getTienAnId());
        temp_TienAnImpl.setUserId(temp_TienAn.getUserId());
        temp_TienAnImpl.setUserName(temp_TienAn.getUserName());
        temp_TienAnImpl.setCreateDate(temp_TienAn.getCreateDate());
        temp_TienAnImpl.setModifiedDate(temp_TienAn.getModifiedDate());
        temp_TienAnImpl.setDoiTuongTienAnId(temp_TienAn.getDoiTuongTienAnId());
        temp_TienAnImpl.setToaAnXuPhat(temp_TienAn.getToaAnXuPhat());
        temp_TienAnImpl.setNgayToaAn(temp_TienAn.getNgayToaAn());
        temp_TienAnImpl.setToiDanh(temp_TienAn.getToiDanh());
        temp_TienAnImpl.setNgayPhamToi(temp_TienAn.getNgayPhamToi());
        temp_TienAnImpl.setXoaAnTich(temp_TienAn.isXoaAnTich());
        temp_TienAnImpl.setChuThich(temp_TienAn.getChuThich());
        temp_TienAnImpl.setHinhPhatChinh(temp_TienAn.getHinhPhatChinh());
        temp_TienAnImpl.setHinhPhatBoSung(temp_TienAn.getHinhPhatBoSung());
        temp_TienAnImpl.setNghiaVuDanSu(temp_TienAn.getNghiaVuDanSu());
        temp_TienAnImpl.setAnPhi(temp_TienAn.getAnPhi());
        temp_TienAnImpl.setBanAnSo(temp_TienAn.getBanAnSo());
        temp_TienAnImpl.setMoTaTienAn(temp_TienAn.getMoTaTienAn());
        temp_TienAnImpl.setUndoRedo(temp_TienAn.getUndoRedo());
        temp_TienAnImpl.setThaoTac(temp_TienAn.getThaoTac());
        temp_TienAnImpl.setMainTienAnId(temp_TienAn.getMainTienAnId());

        return temp_TienAnImpl;
    }

    /**
     * Returns the t e m p_ tien an with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the t e m p_ tien an
     * @return the t e m p_ tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_TienAnException if a t e m p_ tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn findByPrimaryKey(Serializable primaryKey)
        throws NoSuchTEMP_TienAnException, SystemException {
        TEMP_TienAn temp_TienAn = fetchByPrimaryKey(primaryKey);

        if (temp_TienAn == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchTEMP_TienAnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return temp_TienAn;
    }

    /**
     * Returns the t e m p_ tien an with the primary key or throws a {@link qldc.pm.tienan.NoSuchTEMP_TienAnException} if it could not be found.
     *
     * @param tienAnId the primary key of the t e m p_ tien an
     * @return the t e m p_ tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_TienAnException if a t e m p_ tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn findByPrimaryKey(long tienAnId)
        throws NoSuchTEMP_TienAnException, SystemException {
        return findByPrimaryKey((Serializable) tienAnId);
    }

    /**
     * Returns the t e m p_ tien an with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the t e m p_ tien an
     * @return the t e m p_ tien an, or <code>null</code> if a t e m p_ tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        TEMP_TienAn temp_TienAn = (TEMP_TienAn) EntityCacheUtil.getResult(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
                TEMP_TienAnImpl.class, primaryKey);

        if (temp_TienAn == _nullTEMP_TienAn) {
            return null;
        }

        if (temp_TienAn == null) {
            Session session = null;

            try {
                session = openSession();

                temp_TienAn = (TEMP_TienAn) session.get(TEMP_TienAnImpl.class,
                        primaryKey);

                if (temp_TienAn != null) {
                    cacheResult(temp_TienAn);
                } else {
                    EntityCacheUtil.putResult(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
                        TEMP_TienAnImpl.class, primaryKey, _nullTEMP_TienAn);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(TEMP_TienAnModelImpl.ENTITY_CACHE_ENABLED,
                    TEMP_TienAnImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return temp_TienAn;
    }

    /**
     * Returns the t e m p_ tien an with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param tienAnId the primary key of the t e m p_ tien an
     * @return the t e m p_ tien an, or <code>null</code> if a t e m p_ tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienAn fetchByPrimaryKey(long tienAnId)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) tienAnId);
    }

    /**
     * Returns all the t e m p_ tien ans.
     *
     * @return the t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienAn> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the t e m p_ tien ans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_TienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of t e m p_ tien ans
     * @param end the upper bound of the range of t e m p_ tien ans (not inclusive)
     * @return the range of t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienAn> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the t e m p_ tien ans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_TienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of t e m p_ tien ans
     * @param end the upper bound of the range of t e m p_ tien ans (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienAn> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<TEMP_TienAn> list = (List<TEMP_TienAn>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_TEMP_TIENAN);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_TEMP_TIENAN;

                if (pagination) {
                    sql = sql.concat(TEMP_TienAnModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<TEMP_TienAn>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TEMP_TienAn>(list);
                } else {
                    list = (List<TEMP_TienAn>) QueryUtil.list(q, getDialect(),
                            start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the t e m p_ tien ans from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (TEMP_TienAn temp_TienAn : findAll()) {
            remove(temp_TienAn);
        }
    }

    /**
     * Returns the number of t e m p_ tien ans.
     *
     * @return the number of t e m p_ tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_TEMP_TIENAN);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the t e m p_ tien an persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.tienan.model.TEMP_TienAn")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<TEMP_TienAn>> listenersList = new ArrayList<ModelListener<TEMP_TienAn>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<TEMP_TienAn>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(TEMP_TienAnImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
